package com.github.shiqiyue.app.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.github.shiqiyue.app.common.enums.EnableEnum;
import com.github.shiqiyue.app.modules.sys.entity.Role;
import com.github.shiqiyue.app.modules.sys.mapper.RoleMapper;
import com.github.shiqiyue.app.modules.sys.service.IRoleService;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 角色 服务实现类
 * </p>
 *
 * @author wenyao.wu
 * @since 2018-10-16
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {


    @Override
    public Boolean isCodeValid(String code) {
        boolean isCodeValid = this.count(new QueryWrapper<Role>()
                .eq(Role.CODE, code)
                .eq(Role.ENABLE, EnableEnum.ABLE)) == 0;
        return isCodeValid;
    }

    @Override
    public Boolean isCodeValid(String code, Long id) {
        boolean isCodeValid = this.count(new QueryWrapper<Role>()
                .eq(Role.CODE, code)
                .eq(Role.ENABLE, EnableEnum.ABLE)
                .ne(Role.ID, id)) == 0;
        return isCodeValid;
    }

    @Override
    public Boolean delete(Long id, Boolean soft) {
        if (soft) {
            Role updateParam = new Role();
            updateParam.setId(id);
            updateParam.setEnable(EnableEnum.DISABLE);
            return this.updateById(updateParam);
        }
        return this.removeById(id);
    }


}
